<?php

class AdminController extends Controller{
    public $layout='//layouts/admin';
    //public $layout='//layouts/home';

    public function Upload($file){
        $fileupload = $file['file_upload'];
        $tmp=$fileupload['tmp_name'];
        $filename=time().$fileupload['name'];
        $destinal='uploads/admin/'.$filename;
        move_uploaded_file($tmp,$destinal);
        return $destinal;
    }

    public function actionIndex(){
        session_start();
        $this->render('index');
    }
    public function actionPreInsert(){
        session_start();
        if($_SESSION['admin'] && $_SESSION['admin']['lever']==1){
            $this->render('insert_admin');
        }else{
            Yii::app()->user->setFlash('contact','Bạn Phải đăng nhập tài khoản admin');
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }

    public function actionInsert(){
        session_start();
        if($_SESSION['admin'] && $_SESSION['admin']['lever']==1){
            $username = $_POST['username'];
            $password = $_POST['password'];
            $password2 = $_POST['password2'];
            $email = $_POST['email'];
            $lever = $_POST['lever'];
            $avatar = $this->Upload($_FILES);
            $modelAdmin = New Admin();
            $checkuser = $modelAdmin->checkUserExit($username);
            $checkemail = $modelAdmin->checkEmailExit($email);
            if( $password == $password2 && $checkuser == true && $checkemail == true){
                $modelAdmin = New Admin();
                $modelAdmin->username = $username;
                $modelAdmin->password = md5($password);
                $modelAdmin->email = $email;
                $modelAdmin->avatar = $avatar;
                $modelAdmin->lever = $lever;
                $modelAdmin->save();
                $this->redirect('index.php?r=admin/admin/listadmin');
            }else{
                Yii::app()->user->setFlash('contact','User hoặc Email đã bị trùng hoặc có thể Password xác nhận không đúng');
                $this->redirect('index.php?r=admin/admin/index');
            }
        }else{
            Yii::app()->user->setFlash('contact','Bạn Phải đăng nhập tài khoản admin');
            $this->redirect('index.php?r=admin/admin/prelogin');
        }

    }

    public function actionListAdmin(){
        session_start();
//        echo '<pre>';
//        print_r($_SESSION);
//        die;
        if($_SESSION['admin']['lever']==1){
            $modeladmin = new Admin();
            $listadmin = $modeladmin->findAll(array('order'=>'id asc'));
            $this->render('list_admin',array('listadmin'=>$listadmin));
        }else{
            Yii::app()->user->setFlash('contact','bạn không có quyền truy cập chức năng này');
            $this->redirect('index.php?r=admin/admin/index');
        }
    }
    public function actionListMost(){
        session_start();
        if($_SESSION['admin']){
            $modeladmin = new Admin();
            $listadmin = $modeladmin->findAll(array('order'=>'id asc'));
            $this->render('list_most',array('listadmin'=>$listadmin));
        }else{
            Yii::app()->user->setFlash('contact','Bạn Phải đăng nhập tài khoản admin');
            $this->redirect('index.php?r=admin/admin/prelogin');
        }

    }

    public function actionPreEdit(){
        session_start();
        if( is_numeric($_GET['id']) && $_SESSION['admin']['lever']==1){
            $id = $_GET['id'];
            $modelAdmin = new Admin();
            $admin = $modelAdmin->findbypk($id);
            $_SESSION['admin']['id']= $admin->id;
            $_SESSION['admin']['avatar']= $admin->avatar;
            $this->render('edit_admin',array('admin'=>$admin));
        }else{
            Yii::app()->user->setFlash('contact','Đường dẫn không hợp lệ');
            $this->redirect('index.php?r=admin/admin/index');
        }
    }
    public function actionEdit(){
        session_start();
        if($_FILES['file_upload']['name']==!null && $_SESSION['admin']['lever']==1){
            $avatar = $this->Upload($_FILES);
            $id = $_SESSION['admin']['id'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];

            $model = Admin::model()->FindByPk($id);
            $model ->username = $username;
            $model ->password = md5($password);
            $model ->avatar = $avatar;
            $model ->email = $email;
            $check = $model->update();
            if(isset($check)){
                Yii::app()->user->setFlash('contact','cập nhật thành viên đã thành công');
                $this->redirect('index.php?r=admin/admin/index');
            }
        }
        Yii::app()->user->setFlash('contact','cập nhật thành viên không thành công, Không sửa đc');
        $this->redirect('index.php?r=admin/admin/index');
    }
    //sửa đổi user cá nhân
    public function actionPreEditMost(){
        session_start();
        if( is_numeric($_SESSION['admin']['id'])){
            $id = $_SESSION['admin']['id'];
            $modelAdmin = new Admin();
            $admin = $modelAdmin->findbypk($id);
            $_SESSION['admin']['avatar']= $admin->avatar;
            Yii::app()->user->setFlash('contact','Thông tin cá nhân của bạn');
            $this->render('edit_most',array('admin'=>$admin));
        }else{
            Yii::app()->user->setFlash('contact','Bạn không có quyền truy cập');
            $this->redirect('index.php?r=admin/admin/index');
        }
    }
    public function actionEditMost(){
        session_start();
        if($_SESSION['admin']){
            if($_FILES['file_upload']['name']==!null){
                $avatar = $this->Upload($_FILES);
            }else{
                $avatar = $id = $_SESSION['admin']['avatar'];
            }
            $id = $_SESSION['admin']['id'];
            $password = $_POST['password'];
            $checkusername = Admin::model()->CheckUserExit($_POST['username']);
            if ($checkusername==true){
                $username = $_POST['username'];
            }else{
                $username = $_SESSION['admin']['username'];
                Yii::app()->user->setFlash('contact','username bị trùng');
            }
            if(Admin::model()->CheckEmailExit($_POST['email'])){
                $email = $_POST['email'];
            }else{
                $email = $_SESSION['admin']['email'];
                Yii::app()->user->setFlash('contact','email đã trùng');
            }

            $model = Admin::model()->FindByPk($id);
            $model ->username = $username;
            $model ->password = md5($password);
            $model ->avatar = $avatar;
            $model ->email = $email;
            $check = $model->update();
            if(isset($check)){
                Yii::app()->user->setFlash('contact','cập nhật thành viên đã thành công');
                $this->redirect('index.php?r=admin/admin/index');
            }
        }

    }

    public function actionDelete($id){
        session_start();
        if($_SESSION['admin']['lever']==1){
            $model = Admin::model()->FindByPk($id);
            $check = $model->delete();
            if($check){
                $this->redirect('index.php?r=admin/admin/listadmin');
            }
        }else{
            Yii::app()->user->setFlash('contact','bạn phải đăng nhật tài khoản admin');
            $this->redirect('index.php?r=admin/admin/index');
        }

    }
    public function actionPreLogin(){
        session_start();
        session_destroy();
        $this->layout='//layouts/empty';
        $this->render('login_admin');
    }
    public function actionLogin(){
        isset($_POST['username'])?$username = $_POST['username']: $username="";
        isset($_POST['password'])?$password = md5($_POST['password']): $password="";
        $login = Admin::model()->CheckLoginExit($username,$password);
        if($login == true){
            $model= new Admin();
            $admin = $model->findByAttributes(array('username'=>$username));
            $status = $model->findByPk($admin->id);
            $status->status = "1";
            $status->update();
            session_start();
            $_SESSION['admin']['id']= $admin->id;
            $_SESSION['admin']['username']= $admin->username;
            $_SESSION['admin']['password']= $admin->password;
            $_SESSION['admin']['email']= $admin->email;
            $_SESSION['admin']['fullname']= $admin->fullname;
            $_SESSION['admin']['avatar']= $admin->avatar;
            $_SESSION['admin']['lever']= $admin->lever;
            $_SESSION['admin']['status']= $admin->status;
            $this->redirect('index.php?r=admin/admin/listmost');
        }else{
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }
    public function actionLogout(){
        session_start();
        $id = $_SESSION['admin']['id'];
        $model = Admin::model()->findByPk($id);
        $model->status ="0";
        $model->update();
        session_destroy();
        $this->redirect('index.php?r=admin/admin/prelogin');
    }


}